/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import weibo4j.Comments;
import weibo4j.Weibo;
import weibo4j.model.Comment;
import weibo4j.model.Paging;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class commentData {

    
    public static int s1 = 1;//我评论给原微博
    public static int s2 = 1;//我回复某人
    public static int s3 = 1;//我回复某人，但是ID取不到，算作评论给原微博
    public static int s4 = 1;//我在评论中@某人
    public static int s5 = 1;//别人评论我
    public static int s6 = 1;//别人在评论中@我
    public static Date afterDate = Calendar.getInstance().getTime();
    public static final Date today = Calendar.getInstance().getTime();

    public static void commentData(DataPreparation data) throws FileNotFoundException, IOException, WeiboException {
        afterDate.setDate(today.getDate() -Config.iDate);
        commentDataFromMe(data);
        commentToMeData(data);
    }

    public static void commentToMeData(DataPreparation data) throws WeiboException {
        Comments cm = new Comments();
        List<Comment> comments = new ArrayList<Comment>();
        int i = 1;
        while (true) {
            List<Comment> tmp = cm.getCommentToMe(new Paging(i++, 50), 1, 0);
            if(i>80){
                break;
            }
            if (tmp.isEmpty()) {
                break;
            } else {
                comments.addAll(tmp);
            }
        }
        i = 1;
        while (true) {
            List<Comment> tmp = cm.getCommentMentions(new Paging(i++, 50), 1, 0);
            if(i>80){
                break;
            }
            if (tmp.isEmpty()) {
                break;
            } else {
                comments.addAll(tmp);
            }
        }
        for (Comment c : comments) {
            String uid = c.getUser().getId();
            if (c.getCreatedAt().after(afterDate)) {
                if (data.data.latest.containsKey(uid)) {
                    int newc = data.data.latest.get(uid) + 1;
                    data.data.latest.put(uid, newc);
                } else {
                    data.data.latest.put(uid, 1);
                }
            }
            int newc = 0;
            if (data.data.comment.containsKey(uid)) {
                newc = data.data.comment.get(uid);
            }
            newc += 1;
            data.data.comment.put(uid, newc);
        }


    }

    public static void commentDataFromMe(DataPreparation data) throws WeiboException {
        Comments cm = new Comments();

        List<Comment> comments = new ArrayList<Comment>();
        int i = 1;
        while (true) {
            List<Comment> tmp = cm.getCommentByMe(new Paging(i++, 50), 0);
            if(i>80){
                break;
            }
            if (tmp.isEmpty()) {
                break;
            } else {
                comments.addAll(tmp);
            }
        }
        int j = 1;
        for (Comment s : comments) {
            String toStatusUser = s.getStatus().getUser().getId();
            List<String> toReplyUser = StringAnalyse(s.getText());
            if (toReplyUser.isEmpty()) {
                if (s.getCreatedAt().after(afterDate)) {
                    if (data.data.latest.containsKey(toStatusUser)) {
                        int newc = data.data.latest.get(toStatusUser) + 1;
                        data.data.latest.put(toStatusUser, newc);
                    } else {
                        data.data.latest.put(toStatusUser, 1);
                    }
                }
                int newCount = 0;
                int newlatelyCount = 0;
                if (data.data.comment.containsKey(toStatusUser)) {
                    newCount = data.data.comment.get(toStatusUser);
                }
                
                data.data.comment.put(toStatusUser, newCount + 1);

            } else {
                for (String to : toReplyUser) {
                    String toID = data.data.username2id.get(to);

                    if (toID != null) {
                        if (s.getCreatedAt().after(afterDate)) {
                            if (data.data.latest.containsKey(toID)) {
                                int newc = data.data.latest.get(toID) + 1;
                                data.data.latest.put(toID, newc);
                            } else {
                                data.data.latest.put(toID, 1);
                            }
                        }
                        int newCount = 0;
                        int newlatelyCount = 0;
                        if (data.data.comment.containsKey(toID)) {
                            newCount = data.data.comment.get(toID);
                        }
                        
                        data.data.comment.put(toID, newCount + 1);
                    }
                }
            }
        }
    }

    public static List<String> StringAnalyse(String s) {
        List<String> toUser = new ArrayList<String>();
        if (s.startsWith("回复")) {
            int start = s.indexOf("@");
            int end = s.indexOf(":");
            if (start > 0 && end > 0 && end > start) {
                toUser.add(s.substring(start + 1, end));
            }
            String a[] = s.split("@");
            int atCount = a.length;
            if (atCount <= 2) {
                return toUser;
            } else {
                for (int i = 2; i < atCount; i++) {
                    String user = a[i].split(" ")[0];
                    if (!user.isEmpty()) {
                        toUser.add(user);
                    }
                }
            }
            return toUser;
        } else {
            String b[] = s.split("//@");
            String stringPrePost = b[0];
            int atPosition = stringPrePost.indexOf("@");
            if (atPosition >= 0) {
                String[] strings = b[0].split("@");
                int stringsCount = strings.length;
                for (int i = 0; i < stringsCount; i++) {
                    String user = strings[i].split(" ")[0];
                    if (!user.isEmpty()) {
                        toUser.add(user);
                    }
                }
            }
            return toUser;
        }
    }
}
